package com.edooteach.api.web.controller.basic;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.demo2do.core.entity.result.HttpResult;
import com.edoomath.entity.course.Course;
import com.edoomath.entity.mall.Sku;
import com.edooteach.api.servcice.CourseService;
import com.edooteach.api.servcice.SkuService;

@Controller
@RequestMapping("/basic-data")
public class BasicDataController {
    
    private static final Logger logger = LoggerFactory.getLogger(BasicDataController.class);

    @Autowired
    private SkuService skuService;

    @Autowired
    private CourseService courseService;
    
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult load() {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            List<Sku> skus = skuService.listBookSku();
            Map<String, List<Course>> coursesMap = courseService.listBySku(skus);
            
            httpResult.success().data("skus", skus).data("courseMaps", coursesMap);
            
        } catch (Exception e) {
            logger.error("ActivityController#onCreate() error:", e);
            httpResult.fail("500").message("获取所有书籍失败");
            
        }
        
        return httpResult;
    }
    
}
